package cn.iocoder.yudao.module.system.service.merchant;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.StoreProfileReqVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.AppFellowReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.FellowMerchantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.dal.mysql.investment.InvestmentMapper;
import cn.iocoder.yudao.module.system.dal.mysql.investment.ParticipationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.FellowMerchantMapper;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.MerchantMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ALREADY_FOLLOW;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOT_FOLLOW;

@Service
public class MerchantServiceImpl implements MerchantService {
    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private InvestmentMapper investmentMapper;

    @Resource
    private ParticipationMapper participationMapper;
    @Resource
    private FellowMerchantMapper fellowMerchantMapper;

    @Override
    public CommonResult<MerchantDO> getStoreProfile(long id) {
        MerchantDO merchant = merchantMapper.selectById(id);
        return CommonResult.success(merchant);
    }


    @Override
    public void updateStoreprofile(StoreProfileReqVO reqVO) {
        MerchantDO merchant = merchantMapper.selectById(reqVO.getMerchantId());

        if (merchant == null) {
            throw new RuntimeException("商店不存在");
        }

        // 2. 更新商店信息
        // 更新商店名称
        merchant.setMerchantName(reqVO.getMerchantName());
        // 更新主营产品
        merchant.setMainProduct(reqVO.getMainProduct());
        // 更新商店描述
        merchant.setDescription(reqVO.getDescription());
        // 更新地址
        merchant.setContactAddress(reqVO.getContactAddress());

        // 3. 保存更新后的商店信息
        merchantMapper.updateById(merchant);

    }

    /**
     * 参与招商商家获取正在招商的商家
     *
     * @return
     */
    @Override
    public PageResult<MerchantDO> ParticipationGettingMerchantPage(InvestmentReqVO reqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        List<Integer> projectIds = participationMapper.selectProjectIds(userId);
        List<Long> merchantIds = investmentMapper.selectMerchantIdsByInvestmentIds(reqVO, projectIds);
        List<MerchantDO> merchantDOList = merchantMapper.selectListByUserIds(merchantIds);
        return Objects.isNull(merchantDOList) ? null : new PageResult<>(merchantDOList, (long) merchantDOList.size());
    }

    // 招商商家获取正在参与招商中的商家
    @Override
    public PageResult<MerchantDO> InvestmentGettingMerchantPage(ParticipationReqVO reqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        List<Integer> projectIds = investmentMapper.selectInvestmentIdsByMerchantId(reqVO, userId);
        List<Long> merchantIds = participationMapper.selectParticipationIdsByProjectIds(reqVO, projectIds);
        List<MerchantDO> merchantDOList = merchantMapper.selectListByUserIds(merchantIds);
        return Objects.isNull(merchantDOList) ? null : new PageResult<>(merchantDOList, (long) merchantDOList.size());
    }

    @Override
    public MerchantDO getMerchant(Long merchantId) {
        MerchantDO merchant = merchantMapper.selectById(merchantId);
        return merchant;
    }

    @Override
    public MerchantDO getMerchantByUserId(Long userId) {
        return userId!=null?merchantMapper.selectByUserId(userId):null;
    }

    @Override
    public String getMerchantNameById(Long merchantId) {
        if (merchantId == null) {
            throw new IllegalArgumentException("Merchant ID cannot be null.");
        }

        MerchantDO merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            return "未知商家";
        }

        return merchant.getMerchantName();
    }


    @Override
    public void followMerchant(Long merchantId) {
        Long userId = getLoginUserId();
        FellowMerchantDO newLike = new FellowMerchantDO();
        newLike.setMerchantId(merchantId);
        newLike.setUserId(userId);
        FellowMerchantDO exist = fellowMerchantMapper.selectByMerchantAndUser(merchantId, userId);
        if (exist != null) {
            throw exception(ALREADY_FOLLOW);
        }
        fellowMerchantMapper.insert(newLike);

    }

    @Override
    public void unFellow(Long merchantId) {
        Long userId = getLoginUserId();
        FellowMerchantDO exist = fellowMerchantMapper.selectByMerchantAndUser(merchantId, userId);
        if (exist == null) {
            throw exception(NOT_FOLLOW);
        }
        fellowMerchantMapper.deleteByIdAndMerchantId(userId, merchantId);
    }

    @Override
    public PageResult<FellowMerchantDO> getFellowPage(AppFellowReqVO pageReqVO) {
        return fellowMerchantMapper.selectPage(pageReqVO);
    }

    @Override
    public Collection<MerchantDO> getMerchantList(Set<Long> merchantIds) {
        Map<Long, MerchantDO> merchantMap = convertMap(merchantMapper.selectByMerchantIds(merchantIds), MerchantDO::getMerchantId);
        return convertList(merchantIds, merchantMap::get);
    }


    @Override
    public Integer CheckMerchantIsFollow(Long merchantId) {
        Long userId = getLoginUserId();
        FellowMerchantDO exist = fellowMerchantMapper.selectByMerchantAndUser(merchantId, userId);
        if (exist == null) {
            return 0;
        }
        return 1;
    }


}
